#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution 
{
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) 
    {
        vector<vector<int>> result;
        int length = nums.size();
        if(length < 4)
        {
            return result;
        }

        sort(nums.begin(), nums.end());

        for(int i = 0; i < length - 3; i++)
        {
            if(i > 0 && nums[i] == nums[i - 1])
            {
                continue;
            }
            for(int j = i + 1; j < length - 2; j++)
            {
                if(j > i + 1 && nums[j] == nums[j - 1])
                {
                    continue;
                }
                int left = j + 1;
                int right = length - 1;
                while (left < right)
                {
                    long long sum = (long long)nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum < target)
                    {
                        left++;
                    }
                    else if (sum > target)
                    {
                        right--;
                    }
                    else
                    {
                        result.push_back({nums[i], nums[j], nums[left], nums[right]});
                        while (left < right && nums[left] == nums[left + 1])
                        {
                            left++;
                        }
                        while (left < right && nums[right]== nums[right - 1])
                        {
                            right--;
                        }
                        left++;
                        right--;
                    }
                }
                
            }
        }
        return result;
    }
};

int main()
{
    Solution sol;

    // 测试用例1: 经典例子
    vector<int> nums1 = {1, 0, -1, 0, -2, 2};
    int target1 = 0;
    cout << "测试用例1: nums = [1,0,-1,0,-2,2], target = 0" << endl;
    vector<vector<int>> result1 = sol.fourSum(nums1, target1);
    cout << "结果: ";
    for(auto& quadruplet : result1)
    {
        cout << "[";
        for(int i = 0; i < quadruplet.size(); i++)
        {
            cout << quadruplet[i];
            if(i < quadruplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例2: 空数组
    vector<int> nums2 = {2, 2, 2, 2, 2};
    int target2 = 8;
    cout << "测试用例2: nums = [2,2,2,2,2], target = 8" << endl;
    vector<vector<int>> result2 = sol.fourSum(nums2, target2);
    cout << "结果: ";
    for(auto& quadruplet : result2)
    {
        cout << "[";
        for(int i = 0; i < quadruplet.size(); i++)
        {
            cout << quadruplet[i];
            if(i < quadruplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例3: 少于4个元素
    vector<int> nums3 = {1, 2, 3};
    int target3 = 6;
    cout << "测试用例3: nums = [1,2,3], target = 6" << endl;
    vector<vector<int>> result3 = sol.fourSum(nums3, target3);
    cout << "结果: ";
    for(auto& quadruplet : result3)
    {
        cout << "[";
        for(int i = 0; i < quadruplet.size(); i++)
        {
            cout << quadruplet[i];
            if(i < quadruplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例4: 正好4个元素
    vector<int> nums4 = {1, 0, -1, 0};
    int target4 = 0;
    cout << "测试用例4: nums = [1,0,-1,0], target = 0" << endl;
    vector<vector<int>> result4 = sol.fourSum(nums4, target4);
    cout << "结果: ";
    for(auto& quadruplet : result4)
    {
        cout << "[";
        for(int i = 0; i < quadruplet.size(); i++)
        {
            cout << quadruplet[i];
            if(i < quadruplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例5: 负数目标值
    vector<int> nums5 = {-3, -2, -1, 0, 0, 1, 2, 3};
    int target5 = -2;
    cout << "测试用例5: nums = [-3,-2,-1,0,0,1,2,3], target = -2" << endl;
    vector<vector<int>> result5 = sol.fourSum(nums5, target5);
    cout << "结果: ";
    for(auto& quadruplet : result5)
    {
        cout << "[";
        for(int i = 0; i < quadruplet.size(); i++)
        {
            cout << quadruplet[i];
            if(i < quadruplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    return 0;
}
